package com.laiqilin.controller;

import com.laiqilin.entity.Students;
import com.laiqilin.service.IStudentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import utils.PageResult;
import utils.R;

import java.util.List;

@RestController
@RequestMapping("/stu")
public class StudentsController {
    @Autowired
    private IStudentsService studentsService;

    // 学生列表
    @GetMapping("/list.do")
    public List<Students> getAll(){
        return studentsService.list();
    }

    // 分页
    @GetMapping("/selectByPage.do")
    public PageResult<Students> selectByPage(@RequestParam(name = "currentPage",defaultValue = "1") Integer page,
                                             @RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize){
        return studentsService.selectByPage(page,pageSize);
    }

    @PostMapping("/selectByCondition.do")
    public PageResult<Students> selectCondition(@RequestBody(required = false) Students students,
                                                @RequestParam(name = "currentPage",defaultValue = "1") Integer page,
                                                @RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize){
        return studentsService.selectByCondition(page,pageSize,students);
    }

    // 添加学生
    @PostMapping("addStudent.do")
    public R addStudent(@RequestBody Students students){
       return studentsService.save(students)?R.ok().message("添加成功"):R.error().message("添加失败");
    }

    // 删除学生
    @DeleteMapping("/deleteStudent.do")
    public R deleteStudent(@RequestParam("stuId") String stuId){
        return studentsService.deleteStudent(stuId)?R.ok().message("删除成功"):R.error().message("删除失败");
    }

    // 修改信息
    @PutMapping("/updateStudent.do")
    public R updateStudent(@RequestBody Students students){
        return studentsService.updateStudent(students) ? R.ok().message("修改成功") : R.error().message("修改失败");
    }

}
